import type { FastifyInstance } from "fastify";
import { register_fastify } from "typeorm-connection";
import * as entities from "./entities/index.js";

export default async function (fastify: FastifyInstance, opts) {
	fastify.register(import("./plugins/close.js"));
	fastify.register(import("./plugins/sign.js"));
	await fastify.register(import("@fastify/rate-limit"));

	register_fastify(fastify, {
		url: process.env.DATABASE_URL,
		entities: Object.values(entities),
	});

	fastify.setNotFoundHandler(
		{
			preHandler: fastify.rateLimit(),
		},
		(request, reply) => {
			reply.code(404).send("Not Found");
		},
	);

	fastify.register(import("./routes/root.js"));
}
